<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>延缓执行若干秒和纳秒</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.sys-getloadavg.html">« sys_getloadavg</a></li>
      <li style="float: right;"><a href="function.time-sleep-until.html">time_sleep_until »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.misc.html">杂项 函数</a></li>
    <li>延缓执行若干秒和纳秒</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.time-nanosleep" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">time_nanosleep</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">time_nanosleep</span> &mdash; <span class="dc-title">延缓执行若干秒和纳秒</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.time-nanosleep-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>time_nanosleep</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$seconds</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$nanoseconds</code></span>): <span class="type"><span class="type">array</span>|<span class="type">bool</span></span></div>

  <p class="para rdfs-comment">
   程序延缓执行指定数量的 <code class="parameter">seconds</code> 和 <code class="parameter">nanoseconds</code>。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.time-nanosleep-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">seconds</code></dt>

     <dd>

      <p class="para">
       必须是一个非负整数。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">nanoseconds</code></dt>

     <dd>

      <p class="para">
       必须是一个小于1亿的非负整数。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <span class="simpara">
        在 Windows 上，系统的睡眠时间可能会超过指定的纳秒数，具体取决于硬件。
       </span>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.time-nanosleep-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
  <p class="para">
   如果延迟被中断，一个关联数组会返回，内容为：
   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
      <code class="literal">seconds</code>——延迟剩余未执行的秒数
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <code class="literal">nanoseconds</code>——延迟剩余未执行的纳秒数
     </span>
    </li>
   </ul>
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.time-nanosleep-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3464">
    <p><strong>示例 #1 <span class="function"><strong>time_nanosleep()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;小心！如果返回数组，将无法按照预期工作<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">time_nanosleep</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">500000000</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Slept&nbsp;for&nbsp;half&nbsp;a&nbsp;second.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;这个更好：<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">time_nanosleep</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">500000000</span><span style="color: #007700">)&nbsp;===&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Slept&nbsp;for&nbsp;half&nbsp;a&nbsp;second.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;这个最好：<br /></span><span style="color: #0000BB">$nano&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">time_nanosleep</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100000</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$nano&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Slept&nbsp;for&nbsp;2&nbsp;seconds,&nbsp;100&nbsp;microseconds.\n"</span><span style="color: #007700">;<br />}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">$nano&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Sleeping&nbsp;failed.\n"</span><span style="color: #007700">;<br />}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$nano</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$seconds&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$nano</span><span style="color: #007700">[</span><span style="color: #DD0000">'seconds'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$nanoseconds&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$nano</span><span style="color: #007700">[</span><span style="color: #DD0000">'nanoseconds'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Interrupted&nbsp;by&nbsp;a&nbsp;signal.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Time&nbsp;remaining:&nbsp;</span><span style="color: #0000BB">$seconds</span><span style="color: #DD0000">&nbsp;seconds,&nbsp;</span><span style="color: #0000BB">$nanoseconds</span><span style="color: #DD0000">&nbsp;nanoseconds."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.time-nanosleep-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.sleep.html" class="function" rel="rdfs-seeAlso">sleep()</a> - 延缓执行</span></li>
    <li class="member"><span class="function"><a href="function.usleep.html" class="function" rel="rdfs-seeAlso">usleep()</a> - 以指定的微秒数延迟执行</span></li>
    <li class="member"><span class="function"><a href="function.time-sleep-until.html" class="function" rel="rdfs-seeAlso">time_sleep_until()</a> - 使脚本睡眠到指定的时间为止</span></li>
    <li class="member"><span class="function"><a href="function.set-time-limit.html" class="function" rel="rdfs-seeAlso">set_time_limit()</a> - 设置脚本最大执行时间</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>